package org.cocos2dx.javascript;

import android.app.Activity;
import android.content.Context;
import android.util.Log;



import com.vivo.ad.video.ActivityBridge;
import com.vivo.ad.video.VideoAdListener;
import com.vivo.ad.video.VideoAdResponse;
import com.vivo.mobilead.video.VideoAdParams;
import com.vivo.mobilead.video.VivoVideoAd;

import java.util.HashMap;
import java.util.Map;

public class VideoActivity extends Activity {


    private VideoAdResponse mVideoAdResponse;
    private ActivityBridge mActivityBridge;
    private VivoVideoAd mVivoVideoAd;
    public AppActivity app = null;
    private Context context = null;
    private static VideoActivity instance;
    private VideoActivity () {}
    public static VideoActivity getInstance() {
        if (instance == null) {
            instance = new VideoActivity();
        }
        return instance;
    }


    public void initApp (AppActivity _app, Context _context) {
        context=_context;
        app = _app;
        requestVideoAd();
    }

    public void requestVideoAd() {

        VideoAdParams.Builder builder = new VideoAdParams.Builder(Config.RewardId);
        mVivoVideoAd = new VivoVideoAd(app, builder.build(), mVideoAdListener);
        mVivoVideoAd.loadAd();
    }

    public void playVideoAd() {
        if (mVideoAdResponse != null) {
            /**
             * =================这段代码很重要=================
             *
             * mVivoVideoAd.getActivityBridge() 获取ActivityBridge方便
             * 宿主Activity 在onResume、onPause中通知SDK Activity生命周期的变化
             *
             * =================这段代码很重要=================
             */
            mActivityBridge = mVivoVideoAd.getActivityBridge();
            //已经被播放过的广告不能被再次播放,需要重新请求广告对象
            app.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mVideoAdResponse.playVideoAD(app);
                }
            });

        } else {
            Log.d(Config.TAG, "本地没有广告");
          //  showTip("本地没有广告");
        }
    }

    private VideoAdListener mVideoAdListener = new VideoAdListener() {
        @Override
        public void onAdLoad(VideoAdResponse videoADResponse) {
            Log.d(Config.TAG, "广告请求成功");
            mVideoAdResponse = videoADResponse;
        }

        @Override
        public void onAdFailed(String error) {
            Log.d(Config.TAG, "广告请求失败：" + error);
        }

        @Override
        public void onVideoStart() {
        }

        @Override
        public void onVideoCompletion()
        {
            Log.d(Config.TAG, "视频播放完成");
        }

        @Override
        public void onVideoClose(int currentPosition)
        {
            Log.d(Config.TAG, "视频播放被用户中断");
            videoRes(false);
        }

        @Override
        public void onVideoCloseAfterComplete() {
            Log.d(Config.TAG, "视频播放完成，回到游戏界面, 开始发放奖励");
            videoRes(true);
           // showTip("视频播放完成，回到游戏界面, 开始发放奖励");
        }

        @Override
        public void onVideoError(String error) {
            videoRes(false);
            Log.d(Config.TAG, "视频播放错误：" + error);
          //  showTip("视频播放错误：" + error);
        }

        @Override
        public void onFrequency() {
            videoRes(false);
            Log.d(Config.TAG, "广告请求过于频繁");
         //   showTip("广告请求过于频繁");
        }

        @Override
        public void onNetError(String error)
        {
            videoRes(false);
            Log.d(Config.TAG, ""+error);
        }

        @Override
        public void onRequestLimit() {
        }
    };


    public void videoRes(boolean res){
        Map m1 = new HashMap();

        if(res){
            m1.put(Config.wdKeyVideoAdResult, Config.wdVideoAdSuccess);
        }
        else {
            m1.put(Config.wdKeyVideoAdResult, Config.wdVideoAdFail);
        }
        WDNativePlatform.getInstance().videoFinish(m1);
    }

    /**
     * =================这段代码很重要=================
     * <p>
     * 通知 SDK 宿主Activity生命周期变化, SDK 会根据需要 暂停、恢复视频的播放。
     * 如果缺少相关代码会导致 SDK 无法响应生命周期暂停、恢复视频的播放
     * 以及播放完成后不能正确、及时的移除 视频视图
     * <p>
     * =================这段代码很重要=================
     */

    @Override
    protected void onResume() {
        super.onResume();
        if (mActivityBridge != null) {
            mActivityBridge.onResume();
        }
    }

    /**
     * =================这段代码很重要=================
     * <p>
     * 通知 SDK 宿主Activity生命周期变化, SDK 会根据需要 暂停、恢复视频的播放。
     * 如果缺少相关代码会导致 SDK 无法响应生命周期暂停、恢复视频的播放
     * 以及播放完成后不能正确、及时的移除 视频视图
     * <p>
     * =================这段代码很重要=================
     */

    @Override
    protected void onPause() {
        super.onPause();
        if (mActivityBridge != null) {
            mActivityBridge.onPause();
        }
    }

    @Override
    public void onBackPressed() {
        if (mActivityBridge != null) {
            if (mActivityBridge.onBackPressed()) {
                return;
            }
        }
        super.onBackPressed();
    }

}
